﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace GameViewer.Ogre
{
    public class OgreMeshManager : GameViewer.Core.Singleton<OgreMeshManager>
    {
        Dictionary<string, OgreMesh> mMeshes = new Dictionary<string, OgreMesh>();

        public OgreMeshManager()
        {
        }

        public OgreMesh Load(string name)
        {
            if (mMeshes.ContainsKey(name))
            {
                return mMeshes[name];
            }
            BinaryReader reader = new BinaryReader(File.Open(name, FileMode.Open));
            OgreMesh mesh = new OgreMesh();
            mMeshes.Add(name, mesh);
            return Load(reader, mesh);
        }

        public OgreMesh Load(GameViewer.Core.Archives.FileInfo file)
        {
            if (mMeshes.ContainsKey(file.BaseName))
            {
                return mMeshes[file.BaseName];
            }
            BinaryReader reader = new BinaryReader(file.Archive.Open(file.FileName));
            OgreMesh mesh = new OgreMesh();
            mMeshes.Add(file.BaseName, mesh);
            return Load(reader, mesh);
        }

        private OgreMesh Load(BinaryReader reader, OgreMesh mesh)
        {
            //获取Mesh
            Ogre.OgreMeshSerializer ms = new OgreMeshSerializer();
            ms.ImportMesh(reader, mesh);

            //返回
            return mesh;
        }
    }
}
